package boundary;

import java.awt.BorderLayout;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

import store.RecypherDatabase;

public class Recypher extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JMenuBar jJMenuBar = null;
	private JMenu jMenu = null;
	private JMenu jMenu1 = null;
	private JMenuItem jMenuItem = null;
	private String userHome = null;
	private String fileLine = null;
	private String dbName = null;
	private final String configFilename = ".recypher";

	/**
	 * This is the default constructor
	 */
	public Recypher() {
		super();
		initialize();
		userHome = System.getProperty("user.home");
		try {
			BufferedReader br = new BufferedReader(new FileReader(userHome+"/"+configFilename));
			try {
				fileLine = br.readLine();
				String [] splitted = fileLine.split(":");
				dbName = splitted[1];
				
			} catch (IOException ioe) {
				// Non si puo leggere dal file
			}
		} catch (FileNotFoundException fnfe) {
			try {
				File newFile = new File(userHome+"/"+configFilename);
				if (newFile.createNewFile()) {
					PrintWriter pw = new PrintWriter(newFile);
					String newDbName=JOptionPane.showInputDialog(this, "Inserisci nuovo nome per il database", "Nuovo database", JOptionPane.INFORMATION_MESSAGE);
					if (newDbName.length() < 2) {
						JOptionPane.showMessageDialog(this, "Sorry, not valid name.", "Error", JOptionPane.ERROR_MESSAGE, null);
						System.exit(0); // Input error - not valid name
					}
					pw.write("database:"+newDbName);
					pw.close();
				}
				else {
					JOptionPane.showMessageDialog(this, "Sorry, cannot create configuration file.", "Error", JOptionPane.ERROR_MESSAGE, null);
					System.exit(0);
				}
			} catch (IOException ioe) {
				JOptionPane.showMessageDialog(this, "Cannot open "+configFilename, "Error", JOptionPane.ERROR_MESSAGE, null);
			}
		}
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 200);
		this.setJMenuBar(getJJMenuBar());
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
		}
		return jContentPane;
	}

	/**
	 * This method initializes jJMenuBar	
	 * 	
	 * @return javax.swing.JMenuBar	
	 */
	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
			jJMenuBar.add(getJMenu());
			jJMenuBar.add(getJMenu1());
		}
		return jJMenuBar;
	}

	/**
	 * This method initializes jMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getJMenu() {
		if (jMenu == null) {
			jMenu = new JMenu();
			jMenu.setText("File");
			jMenu.add(getJMenuItem());
		}
		return jMenu;
	}

	/**
	 * This method initializes jMenu1	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getJMenu1() {
		if (jMenu1 == null) {
			jMenu1 = new JMenu();
			jMenu1.setText("Edit");
		}
		return jMenu1;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItem() {
		if (jMenuItem == null) {
			jMenuItem = new JMenuItem();
			jMenuItem.setText("Submenu");
		}
		return jMenuItem;
	}

}
